import gc
import network
import time
from socket import *
import micropython as mpn

# 连接热点
def do_connect():
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print('connecting to network...')
        wlan.connect('meik30', '5201314ygyu')  # 配置热点名称和密码
        i = 1
        while not wlan.isconnected():
            print("正在链接...{}".format(i))
            i += 1
            time.sleep(1)
    print('network config:', wlan.ifconfig())

do_connect()

# 内存回收，防止数据溢出
def mem_recov():
    size = gc.mem_alloc()
    men_size = gc.mem_free()
    if size > (men_size // 2):
        gc.collect()
    

def introd_data():
    image_data = socket(AF_INET, SOCK_DGRAM, 0)  # udp接受视频流
    addr = ('0.0.0.0', 7788)
    image_data.bind(addr)
    while True:
        data, data_size = image_data.recvfrom(2000000)  # udp接受视频数据
        print(data, '\n', data_size)
        mem_recov()

if __name__ == '__main__':
    gc.enable()
    introd_data()
